var AUTH_SUB_REQUEST_URL = "http://www.google.com/accounts/AuthSubRequest";
var token;

function sendData() {
	var requester = new Requester();
	var responder = new Responder("POST");
	var url = "http://www.google.com/base/feeds/items";
	var bodyValue = document.getElementById("inputXML").value;
	requester.request("POST", url, bodyValue, responder,"POST");
}


function updateSingleUseToken(url) {
  var token = getTokenFromUrl(url);
  setTokenCookie(token);
  document.getElementById("singleUseToken").innerHTML = token;
  //document.getElementById("authSubUrl").onclick = "getAuthSubUrl(document.URL)";
}

function getTokenFromUrl(url) {
  var queryString = getQueryStringFromUrl(url);
  var pairs = queryString.split("&");
  for (var i = 0; i < pairs.length; i++) {
    pair = pairs[i].split("=");
    if (pair[0] == "token") {
      return pair[1];
    }
  }
  return "";
}

function getQueryStringFromUrl(url) {
  url = url.split("#")[0];
  var queryString = "";
  var questionMarkIndex = url.indexOf("?");
  if (questionMarkIndex > -1) {
    queryString = url.substring(questionMarkIndex + 1);
  }
  return queryString;
}

function trim(string) {
  return string.replace(/^\s+|\s+$/g, "");
}

function getAuthSubUrl(documentUrl) {
  var nextUrl = "http://www.google.com/ig";
  var scopeUrl = getAuthSubScope(documentUrl);
  window.location =  AUTH_SUB_REQUEST_URL +
      "?next=" + encodeURIComponent(nextUrl) +
      "&scope=" + encodeURIComponent(scopeUrl) +
      "&session=1";
}




function getAuthSubScope(documentUrl) {
  var scopeUrlEndIndex = documentUrl.length + 1;
  var indexOfDoubleSlash = documentUrl.indexOf("//");
  var indexOfSlashAfterDomain =
    documentUrl.indexOf("/", indexOfDoubleSlash + 2);
  if (indexOfSlashAfterDomain > -1) {
    scopeUrlEndIndex = indexOfSlashAfterDomain;
  }
  return "http://www.google.com/base/feeds/items";
}

function getTokenFromUrl() {
  var url = window.location.toString();
  var index = url.indexOf("token=");
  if (index == -1) {
    return "";
  }
  // Search for end of token (& or %26 if escaped)
  var nextIndex;
  var nextIndex1 = url.indexOf("&", index);
  var nextIndex2 = url.indexOf("%26", index);
  if (nextIndex1 == -1) {
    nextIndex = nextIndex2;
  } else if (nextIndex2 == -1) {
    nextIndex = nextIndex1;
  } else {
    nextIndex = Math.min(nextIndex1,nextIndex1);
  }
  url.indexOf("&", index);
  token = (nextIndex == -1)
            ? url.substring(index + 6, url.length)
            : url.substring(index + 6, nextIndex);
  return token;
}



function initAuth() {
	
	updateSingleUseToken(document.URL);
	token = getTokenFromUrl();
}

